Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
What causes the "Argument expression expected" error in this Angular 13 app?

I am working on an e-commerce app who's front-end is made in Angular 13.

The below code is intended to sum the prices of the items in the cart:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: '.app-top-cart',
  templateUrl: './top-cart.component.html',
  styleUrls: ['./top-cart.component.css']
})
export class TopCartComponent implements OnInit {
  cartItems: any = [
    {
      id: 1,
      title: "iPhone 9",
      description: "An apple mobile which is nothing like apple",
      price: 549,
      discountPercentage: 12.96,
      rating: 4.69,
      stock: 94,
      brand: "Apple",
      category: "smartphones",
      thumbnail: "https://dummyjson.com/image/i/products/1/thumbnail.jpg",
      images: [
        "https://dummyjson.com/image/i/products/1/1.jpg",
        "https://dummyjson.com/image/i/products/1/2.jpg",
      ]
    },
    {
      id: 2,
      title: "iPhone X",
      description: "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...",
      price: 899,
      discountPercentage: 17.94,
      rating: 4.44,
      stock: 34,
      brand: "Apple",
      category: "smartphones",
      thumbnail: "https://dummyjson.com/image/i/products/2/thumbnail.jpg",
      images: [
        "https://dummyjson.com/image/i/products/2/1.jpg",
        "https://dummyjson.com/image/i/products/2/2.jpg",
      ]
    }
  ];

  constructor() { }

  totalPrice: number = 0;

  doTotalPrice(){
    let total = 0,
    this.cartItems.forEach((item: { price: number, quantity: number; }) => {
      total += item.price * item.quantity
    }),
    this.totalPrice = total,
 }

  ngOnInit(): void {
    this.doTotalPrice();
  }

}

The problem

The code above does not compile. It gives the error Argument expression expected on line 54, where the doTotalPrice method closes.

Where is my mistake?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

doTotalPrice(){
    let total = 0,
    this.cartItems.forEach((item: { price: number, quantity: number; }) => {
      total += item.price * item.quantity
    },
    this.totalPrice = total,
 }

You do not close your function and you use commas instead of semicolons.

doTotalPrice(){
    let total = 0,
    this.cartItems.forEach((item: { price: number, quantity: number; }) => {
      total += item.price * item.quantity
    });
    this.totalPrice = total;
 }

Bonus, cleaner code :

this.totalPrice = this.cartItems.reduce(
  (p, { price, quantity }) => p + price * quantity, 
  0
);
about 4 years ago · Juan Pablo Isaza Relatório

0

you must delete the comma after the forEach();

 yourArray.forEach(element=>{
 //do something 
 });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda